package com.neusoft.controller;

import com.neusoft.entity.UmsAdmin;
import com.neusoft.service.UmsAdminService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 后台用户表(UmsAdmin)表控制层
 *
 * @author makejava
 * @since 2020-11-02 20:13:32
 */
@Controller
@RequestMapping("umsAdmin")
public class UmsAdminController {
    /**
     * 服务对象
     */
    @Resource
    private UmsAdminService umsAdminService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("queryById")
    public String queryById(Long id) {
        System.out.println(id);
        System.out.println(this.umsAdminService.queryById(id));
        return "index";
    }

    @RequestMapping(value = "login", method = RequestMethod.POST)
    public String login(String username, String password) {
        System.out.println(username + ", " + password);
        return "index";
    }

    @RequestMapping(value = "insert", method = RequestMethod.POST)
    public String insert(UmsAdmin umsAdmin) {
        System.out.println(umsAdmin);
        return "index";
    }

    @GetMapping("toLogin")
    public String toLogin() {
        System.out.println("toLogin");
        return "login";
    }

    @GetMapping("toReg")
    public String toReg() {
        return "reg";
    }

//    @GetMapping("toEdit")//成功
//    public String toEdit(Long id, Model model) {
//        System.out.println("id:::" + id);
//        UmsAdmin bean = umsAdminService.queryById(id);
//        model.addAttribute("bean", bean);
//        return "edit";
//    }

    @GetMapping("toEdit")//成功
    public String toEdit(Long id, Map<String, Object> model) {
        System.out.println("Map<String, Object>---id:::" + id);
        UmsAdmin bean = umsAdminService.queryById(id);
        model.put("bean", bean);
        model.put("name", "haha");
        return "edit";
    }

//    @GetMapping("toEdit")
//    public String toEdit(Long id) {
//        System.out.println("id:::" + id);
//        UmsAdmin bean = umsAdminService.queryById(id);
//        Model model = new ExtendedModelMap();失败
//        model.addAttribute("bean", bean);
//        return "edit";
//    }

//    @GetMapping("toEdit")//成功
//    public ModelAndView toEdit(Long id) {
//        System.out.println("id:::" + id);
//        UmsAdmin bean = umsAdminService.queryById(id);
//        ModelAndView mav = new ModelAndView("edit");
//        mav.getModel().put("bean", bean);
//        return mav;
//    }

    @GetMapping("test")
    @ResponseBody
    public String test() {
        return "{content:\"测试内容\"}";
    }

    @GetMapping("bean")
    @ResponseBody
    public UmsAdmin bean() {
        return umsAdminService.queryById(1L);
    }

//    @GetMapping("jackson")
//    @ResponseBody
//    public String jackson() throws JsonProcessingException {
//        UmsAdmin bean = umsAdminService.queryById(1L);
//        ObjectMapper om = new ObjectMapper();
//
//        return om.writeValueAsString(bean);
//    }


}